#include "loginpage.h"
#include "ui_loginpage.h"
//#include "request.h"
//#include "loginrequest.h"
#include <QString>
#include <QJsonObject>
#include <QJsonDocument>
QString token;
loginPage::loginPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::loginPage)
{
    ui->setupUi(this);
    this->m_lr=new LoginRequest;
    this->m_dr=new DishRequest;
    this->m_fc=new FoodController;
    accessManager = new QNetworkAccessManager(this);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");//构建json头
    //测试post
    //reply = accessManager->post(request, QByteArray("{\"password\": \"123456\", \"phone\": \"15683141454\"}"));
    //m_r=Request::getinstance();
    //m_lr=LoginRequest::getinstance();

    auto f=[&]{
        qDebug()<<"shoudao";
        qDebug()<<m_lr->loginInfo.code;
        qDebug()<<m_lr->loginInfo.message;
        qDebug()<<m_lr->loginInfo.token;
        //qDebug()<<m_dr->DishList[0].detail;
    };
    connect(m_lr,&LoginRequest::LoginByPasswordSuccess,this,f);
    //connect(m_dr,&DishRequest::getDishListSuccess,this,f);
}

loginPage::~loginPage()
{
    delete ui;
}

void loginPage::on_loginBtn_clicked()
{
    code=99999;
    token="";
    qDebug("click登录");
    //数据库校验手机号与验证码才能登录
    QString userName = ui->phoneEdit->text();
    QString pwd = ui->verifyEdit->text();

    //rt.getLoginInfo(userName,pwd);
    //m_r->LoginByPassword(userName,pwd);
    m_lr->getLoginInfo(userName,pwd);

    QJsonObject json;
    json.insert("phone",userName);
    json.insert("password",pwd);

    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    request.setUrl(QUrl("http://81.70.102.35:8082/user/auth/loginByPassword"));//设置url
    reply = accessManager->post(request, postData);//发送POST请求

    //QString postBody = "{\"password\": \""+pwd+"\", \"phone\": \""+userName+"\"}""";
    //request.setUrl(QUrl("http://81.70.102.35:8082/user/auth/loginByPassword"));//设置url
    //postData = postBody.toLatin1();//QString转QByteArray
    //reply = accessManager->post(request, postData);//发送POST请求
    //失败就提示
    //QByteArray bytes = reply->readAll();//此处为空字节

    //qDebug()<<token;
    if(code==0)
    {
        //qDebug()<<"密码正确";
        emit sendLoginSuccess();
    }


    //成功进入主界面


}


void loginPage::on_visitorBtn_clicked()
{
    //m_dr->getDishList("1604d63dd0154400afb10171b932d9be");
    //m_fc->addFood("407af511889b40cdb28a482f65704a80","wbb","wbb","/img/food/14986c2973f04ce9a7a3549f19097bee.jpg","999",2,"克",1,"鱼肉");
    //m_fc->infoFood("407af511889b40cdb28a482f65704a80",3);
    m_fc->searchCustomerFood("9d14760fad0841f2a00286cabb927032","wb",1,15);
}

void loginPage::finishedSlot(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
        {
            QByteArray bytes = reply->readAll();
            //qDebug() << "许凡楠大帅哥";
            //qDebug() << bytes;
            QJsonParseError jsonError;
            QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);  // 转化为 JSON 文档
            QJsonObject object = doucment.object();
            //qDebug()<<object;
            if (object.contains("code"))
            {
                QJsonValue value = object.value("code");  // 获取指定 key 对应的 value
                code=value.toInt();
            }
            if(object.contains("data"))
            {
                QJsonValue value = object.value("data");
                if(value.isObject())
                {
                    QJsonObject obj = value.toObject();
                    if(obj.contains("token"))
                    {
                        QJsonValue value = obj.value("token");
                        token=value.toString();
                        //qDebug()<<token;
                    }
                }
            }
            if(code==0)
            {
                //qDebug()<<"密码正确";
                emit sendLoginSuccess();
            }
        }
        else
        {
            qDebug() << "finishedSlot errors here";
            qDebug( "found error .... code: %d\n", (int)reply->error());
            qDebug(qPrintable(reply->errorString()));
        }
        reply->deleteLater();
}


